package com.example.design.ddd.pingandchong.chong.example3.Service;

import com.example.design.ddd.pingandchong.chong.example3.Customer;
import com.example.design.ddd.pingandchong.chong.example3.Order;
import com.example.design.ddd.pingandchong.chong.example3.OrderItem;
import com.example.design.ddd.pingandchong.chong.example3.factory.OrderFactory;
import com.example.design.ddd.pingandchong.chong.example3.repository.CustomerRepository;
import com.example.design.ddd.pingandchong.chong.example3.repository.OrderRepository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public class OrderService {
    private OrderFactory orderFactory;
    private OrderRepository orderRepository;
    private CustomerRepository customerRepository;
  
    // 构造函数注入依赖项  
    public OrderService(OrderFactory orderFactory, OrderRepository orderRepository, CustomerRepository customerRepository) {  
        this.orderFactory = orderFactory;  
        this.orderRepository = orderRepository;  
        this.customerRepository = customerRepository;  
    }  
  
    @Transactional // 假设使用Spring框架的事务管理注解
    public void placeOrder(Long customerId, List<OrderItem> items) {
        Customer customer = customerRepository.findById(customerId);
        if (customer == null) {  
            throw new IllegalArgumentException("Customer not found");  
        }  
        Order order = orderFactory.createOrder(customer, items);
        orderRepository.save(order);  
    }  
}